<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 数字(Number)</h1>
        <p>Python 数字数据类型用于存储数值。</p>
        <p>数据类型是不允许改变的,这就意味着如果改变数字数据类型得值，将重新分配内存空间。</p>

        <p>以下实例在变量赋值时 Number 对象将被创建：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">var1 </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">1</span><span class="pln">
var2 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">10</span></pre>
        <p>您也可以使用del语句删除一些数字对象的引用。 </p>
        <p>del语句的语法是：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">del</span><span class="pln"> var1</span><span
                class="pun">[,</span><span class="pln">var2</span><span class="pun">[,</span><span
                class="pln">var3</span><span class="pun">[....,</span><span class="pln">varN</span><span class="pun">]]]]</span></pre>
        <p>您可以通过使用del语句删除单个或多个对象的引用，例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">del</span><span class="pln"> </span><span
                class="kwd">var</span><span class="pln">
</span><span class="kwd">del</span><span class="pln"> var_a</span><span class="pun">,</span><span
                class="pln"> var_b</span></pre>
        <p>Python 支持三种不同的数值类型：</p>
        <ul>
            <li><strong>整型(Int)</strong> - 通常被称为是整型或整数，是正或负整数，不带小数点。Python3 整型是没有限制大小的，可以当作 Long 类型使用，所以 Python3 没有
                Python2 的 Long 类型。
            </li>

            <li><strong>浮点型(float)</strong> - 浮点型由整数部分与小数部分组成，浮点型也可以使用科学计数法表示（2.5e2 = 2.5 x 10<sup>2</sup> = 250）</li>
            <li><strong>复数( (complex))</strong> - 复数由实数部分和虚数部分构成，可以用a + bj,或者complex(a,b)表示， 复数的实部a和虚部b都是浮点型。</li>
        </ul>
        <p>我们可以使用十六进制和八进制来代表整数：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> number </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">0xA0F</span><span class="pln"> </span><span class="com"># 十六进制</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> number
</span><span class="lit">2575</span><span class="pln">

</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> number</span><span class="pun">=</span><span class="lit">0o37</span><span
                class="pln"> </span><span class="com"># 八进制</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> number
</span><span class="lit">31</span></pre>
        <table class="reference">
            <tbody>
            <tr>
                <th>int</th>
                <th>float</th>
                <th>complex</th>
            </tr>
            <tr>
                <td>10</td>
                <td>0.0</td>
                <td>3.14j</td>
            </tr>
            <tr>
                <td>100</td>
                <td>15.20</td>
                <td>45.j</td>
            </tr>
            <tr>
                <td>-786</td>
                <td>-21.9</td>
                <td>9.322e-36j</td>
            </tr>
            <tr>
                <td>080</td>
                <td>32.3+e18</td>
                <td>.876j</td>
            </tr>
            <tr>
                <td>-0490</td>
                <td>-90.</td>
                <td>-.6545+0J</td>
            </tr>
            <tr>
                <td>-0x260</td>
                <td>-32.54e100</td>
                <td>3e+26J</td>
            </tr>
            <tr>
                <td>0x69</td>
                <td>70.2-E12</td>
                <td>4.53e-7j</td>
            </tr>
            </tbody>
        </table>
        <ul>

            <li>Python支持复数，复数由实数部分和虚数部分构成，可以用a + bj,或者complex(a,b)表示，
                复数的实部a和虚部b都是浮点型。
            </li>
        </ul>
        <br>

        <h2>Python 数字类型转换</h2>
        <p>有时候，我们需要对数据内置的类型进行转换，数据类型的转换，你只需要将数据类型作为函数名即可。</p>
        <ul class="list">
            <li><p><b>int(x)</b> 将x转换为一个整数。</p></li>
            <li><p><b>float(x)</b> 将x转换到一个浮点数。</p></li>
            <li><p><b>complex(x)</b> 将x转换到一个复数，实数部分为 x，虚数部分为 0。</p></li>
            <li><p><b>complex(x, y)</b> 将 x 和 y 转换到一个复数，实数部分为 x，虚数部分为 y。x 和 y 是数字表达式。</p></li>
        </ul>
        <p>以下实例将浮点数变量 a 转换为整数：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">1.0</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">int</span><span
                class="pun">(</span><span class="pln">a</span><span class="pun">)</span><span class="pln">
</span><span class="lit">1</span></pre>

        <h2>Python 数字运算</h2>
        <p>Python 解释器可以作为一个简单的计算器，您可以在解释器里输入一个表达式，它将输出表达式的值。</p>
        <p>表达式的语法很直白： +, -, * 和 / 和其它语言（如Pascal或C）里一样。例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">+</span><span
                class="pln"> </span><span class="lit">2</span><span class="pln">
</span><span class="lit">4</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">50</span><span
                class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="lit">5</span><span
                class="pun">*</span><span class="lit">6</span><span class="pln">
</span><span class="lit">20</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">(</span><span
                class="lit">50</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="lit">5</span><span class="pun">*</span><span class="lit">6</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span class="lit">4</span><span
                class="pln">
</span><span class="lit">5.0</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">8</span><span
                class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span class="lit">5</span><span
                class="pln">  </span><span class="com"># 总是返回一个浮点数</span><span class="pln">
</span><span class="lit">1.6</span></pre>
        <p><strong>注意：</strong>在不同的机器上浮点运算的结果可能会不一样。</p>
        <p>在整数除法中，除法（/）总是返回一个浮点数，如果只想得到整数的结果，丢弃可能的分数部分，可以使用运算符 <b>//</b> ：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="lit">17</span><span class="pln"> </span><span class="pun">/</span><span
                class="pln"> </span><span class="lit">3</span><span class="pln">  </span><span
                class="com"># 整数除法返回浮点型</span><span class="pln">
</span><span class="lit">5.666666666666667</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">17</span><span
                class="pln"> </span><span class="com">// 3  # 整数除法返回向下取整后的结果</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">17</span><span
                class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="lit">3</span><span
                class="pln">  </span><span class="com"># ％操作符返回除法的余数</span><span class="pln">
</span><span class="lit">2</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">5</span><span
                class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit">3</span><span
                class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="lit">2</span><span
                class="pln">
</span><span class="lit">17</span></pre>
        <p>
            等号（=）用于给变量赋值。赋值之后，除了下一个提示符，解释器不会显示任何结果。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> width </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">20</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> height </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">5</span><span class="pun">*</span><span class="lit">9</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> width </span><span class="pun">*</span><span class="pln"> height
</span><span class="lit">900</span></pre>
        <p>Python 可以使用 <b>**</b> 操作来进行幂运算：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">**</span><span
                class="pln"> </span><span class="lit">2</span><span class="pln">  </span><span
                class="com"># 5 的平方</span><span class="pln">
</span><span class="lit">25</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">2</span><span
                class="pln"> </span><span class="pun">**</span><span class="pln"> </span><span class="lit">7</span><span
                class="pln">  </span><span class="com"># 2的7次方</span><span class="pln">
</span><span class="lit">128</span></pre>
        <p>变量在使用前必须先"定义"（即赋予变量一个值），否则会出现错误：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> n   </span><span class="com"># 尝试访问一个未定义的变量</span><span class="pln">
</span><span class="typ">Traceback</span><span class="pln"> </span><span class="pun">(</span><span class="pln">most recent call </span><span
                class="kwd">last</span><span class="pun">):</span><span class="pln">
  </span><span class="typ">File</span><span class="pln"> </span><span class="str">"&lt;stdin&gt;"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="str">&lt;module&gt;</span><span class="pln">
</span><span class="typ">NameError</span><span class="pun">:</span><span class="pln"> name </span><span
                class="str">'n'</span><span class="pln"> </span><span class="kwd">is</span><span
                class="pln"> </span><span class="kwd">not</span><span class="pln"> </span><span
                class="kwd">defined</span></pre>
        <p>不同类型的数混合运算时会将整数转换为浮点数：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="pun">*</span><span
                class="pln"> </span><span class="lit">3.75</span><span class="pln"> </span><span
                class="pun">/</span><span class="pln"> </span><span class="lit">1.5</span><span class="pln">
</span><span class="lit">7.5</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="lit">7.0</span><span
                class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span class="lit">2</span><span
                class="pln">
</span><span class="lit">3.5</span></pre>
        <p>
            在交互模式中，最后被输出的表达式结果被赋值给变量 <b>_ </b>。例如：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> tax </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">12.5</span><span class="pln"> </span><span class="pun">/</span><span
                class="pln"> </span><span class="lit">100</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> price </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">100.50</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> price </span><span class="pun">*</span><span class="pln"> tax
</span><span class="lit">12.5625</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> price </span><span class="pun">+</span><span class="pln"> _
</span><span class="lit">113.0625</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> round</span><span class="pun">(</span><span
                class="pln">_</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">)</span><span class="pln">
</span><span class="lit">113.06</span></pre>
        <p>此处， <b>_ </b>变量应被用户视为只读变量。
        </p>

        <br>

        <h2>数学函数</h2>
        <table class="reference">
            <tbody>
            <tr>
                <th>函数</th>
                <th>返回值 ( 描述 )</th>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-abs.html">abs(x)</a></td>
                <td>返回数字的绝对值，如abs(-10) 返回 10</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-ceil.html">ceil(x) </a></td>
                <td>返回数字的上入整数，如math.ceil(4.1) 返回 5</td>
            </tr>
            <tr>
                <td><p>cmp(x, y)</p></td>
                <td>如果 x &lt; y 返回 -1, 如果 x == y 返回 0, 如果 x &gt; y 返回 1。 <strong>Python 3 已废弃</strong> 。使用 <strong> 使用
                    (x&gt;y)-(x&lt;y)</strong> 替换。
                </td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-exp.html">exp(x) </a></td>
                <td>返回e的x次幂(e<sup>x</sup>),如math.exp(1) 返回2.718281828459045</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-fabs.html">fabs(x)</a></td>
                <td>返回数字的绝对值，如math.fabs(-10) 返回10.0</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-floor.html">floor(x) </a></td>
                <td>返回数字的下舍整数，如math.floor(4.9)返回 4</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-log.html">log(x) </a></td>
                <td>如math.log(math.e)返回1.0,math.log(100,10)返回2.0</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-log10.html">log10(x) </a></td>
                <td>返回以10为基数的x的对数，如math.log10(100)返回 2.0</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-max.html">max(x1, x2,...) </a></td>
                <td>返回给定参数的最大值，参数可以为序列。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-min.html">min(x1, x2,...) </a></td>
                <td>返回给定参数的最小值，参数可以为序列。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-modf.html">modf(x) </a></td>
                <td>返回x的整数部分与小数部分，两部分的数值符号与x相同，整数部分以浮点型表示。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-pow.html">pow(x, y)</a></td>
                <td> x**y 运算后的值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-round.html">round(x [,n])</a></td>
                <td>返回浮点数x的四舍五入值，如给出n值，则代表舍入到小数点后的位数。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-sqrt.html">sqrt(x) </a></td>
                <td>返回数字x的平方根。</td>
            </tr>
            </tbody>
        </table>
        <br>

        <h2>随机数函数</h2>
        <p>随机数可以用于数学，游戏，安全等领域中，还经常被嵌入到算法中，用以提高算法效率，并提高程序的安全性。</p>
        <p>Python包含以下常用随机数函数：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>函数</th>
                <th>描述</th>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-choice.html">choice(seq)</a></td>
                <td>从序列的元素中随机挑选一个元素，比如random.choice(range(10))，从0到9中随机挑选一个整数。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-randrange.html">randrange ([start,] stop
                    [,step]) </a></td>
                <td>从指定范围内，按指定基数递增的集合中获取一个随机数，基数缺省值为1</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-random.html">random() </a></td>
                <td> 随机生成下一个实数，它在[0,1)范围内。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-seed.html">seed([x]) </a></td>
                <td>改变随机数生成器的种子seed。如果你不了解其原理，你不必特别去设定seed，Python会帮你选择seed。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-shuffle.html">shuffle(lst) </a></td>
                <td>将序列的所有元素随机排序</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-uniform.html">uniform(x, y)</a></td>
                <td>随机生成下一个实数，它在[x,y]范围内。</td>
            </tr>
            </tbody>
        </table>
        <br>

        <h2>三角函数</h2>
        <p>
            Python包括以下三角函数：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>函数</th>
                <th>描述</th>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-acos.html">acos(x)</a></td>
                <td>返回x的反余弦弧度值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-asin.html">asin(x)</a></td>
                <td>返回x的反正弦弧度值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-atan.html">atan(x)</a></td>
                <td>返回x的反正切弧度值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-atan2.html">atan2(y, x)</a></td>
                <td>返回给定的 X 及 Y 坐标值的反正切值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-cos.html">cos(x)</a></td>
                <td>返回x的弧度的余弦值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-hypot.html">hypot(x, y)</a></td>
                <td>返回欧几里德范数 sqrt(x*x + y*y)。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-sin.html">sin(x)</a></td>
                <td>返回的x弧度的正弦值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-tan.html">tan(x)</a></td>
                <td>返回x弧度的正切值。</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-degrees.html">degrees(x)</a></td>
                <td>将弧度转换为角度,如degrees(math.pi/2) ， 返回90.0</td>
            </tr>
            <tr>
                <td><a target="_blank" href="/python3/python3-func-number-radians.html">radians(x)</a></td>
                <td>将角度转换为弧度</td>
            </tr>
            </tbody>
        </table>
        <br>

        <h2>数学常量</h2>
        <table class="reference">
            <tbody>
            <tr>
                <th>常量</th>
                <th>描述</th>
            </tr>
            <tr>
                <td>pi</td>
                <td>数学常量 pi（圆周率，一般以π来表示）</td>
            </tr>
            <tr>
                <td>e</td>
                <td>数学常量 e，e即自然常数（自然常数）。</td>
            </tr>
            </tbody>
        </table>

    </div>
</div>
</body>
</html>
